package BLL;


import DAL.LogDao;
import DAL.ManagerDao;
import DAL.OperatorDao;

import Entity.Journal;
import Entity.Manager;
import Entity.Operator;
import Utils.TSUtility;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/*
 *
 * @Author Chen
 * @Date 2022/1/9 22:21
 * @Version 1.0
 */

public class LoginService {
    //所有操作涉及用户名，需先调用Exist判断用户名是否存在

    //用单例模式初始化
    private static ManagerDao managerDao = null;
    private static OperatorDao operatorDao = null;
    private static LogDao logDao = null;

    static {
        if (managerDao == null) {
            managerDao = new ManagerDao();
        }
        if (operatorDao == null) {
            operatorDao = new OperatorDao();
        }
        if (logDao == null) {
            logDao = new LogDao();
        }
    }

    //注册 管理员注册功能
    public static int addManager(String accountNumber, String PassWord){
        int result = 0;
        result = managerDao.addManager(accountNumber,PassWord);
        if (result == 1) {

            System.out.println("请输入注册年份：");
            int year= TSUtility.readInt_year();
            year=year-1900;
            System.out.println("请输入注册月份：");
            int mouth=TSUtility.readInt_mouth();
            mouth=mouth-1;
            System.out.println("请输入注册日：");
            int day=TSUtility.readInt_day();
            Date publicationDate=new Date(year,mouth,day);

            logDao.insertloginLog(publicationDate, accountNumber+"注册管理员账号");
            System.out.println("注册成功");
        }
        return result;
    }

    //注册 操作员
    public static int addOperator(String accountNumber,String name, String PassWord){
        int result = 0;
        result = operatorDao.addOperator(accountNumber,PassWord,name);
        if (result == 1) {

            System.out.println("请输入注册年份：");
            int year= TSUtility.readInt_year();
            year=year-1900;
            System.out.println("请输入注册月份：");
            int mouth=TSUtility.readInt_mouth();
            mouth=mouth-1;
            System.out.println("请输入注册日：");
            int day=TSUtility.readInt_day();
            Date publicationDate=new Date(year,mouth,day);
            logDao.insertloginLog(publicationDate, name+"注册操作员账号");
            System.out.println("注册成功");
        }
        return result;
    }
    //管理员登录功能
    public static int Login(Manager manage,String PassWord) {

        if (manage.getPassword().equals(PassWord) ) {
            System.out.println("登录成功");
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");

            logDao.insertloginLog(dateFormat.format(date),"管理员：" + manage.getName()+" 登录");
            return 1;
        } else {
            System.out.println("密码错误!");
        }
        return 0;
    }

    //操作员登录功能
    public static int operatorLogin(Operator operator, String PassWord)  {
        if (operator.getPassword().equals(PassWord)) {
            System.out.println("登录成功");
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            logDao.insertloginLog(dateFormat.format(date),"操作员" + operator.getAccountNumber()+ "登录");
            return 1;
        } else {
            System.out.println("密码错误");
        }
        return 0;
    }

    //修改密码,修改管理员密码
    public static int updateManagerPassWord(String number, String PassWord, String newPassWord){
        int result = 0;
        Manager manager = managerDao.selectBYManager(number);
        if (PassWord.equals(manager.getPassword())) {
            result =  managerDao.updateManager(newPassWord, manager.getAccountNumber());
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            logDao.insertloginLog(dateFormat.format(date) , "管理员ID为：" + number+"密码修改");
        }else{
            System.out.println("原密码错误！");
        }
        return result;
    }

    // 修改操作员密码
    public static int updateOperatorPassWord(String operatorName, String operatorPassWord, String newPassWord){
        int result = 0;
        Operator operator = operatorDao.selectBYOperator(operatorName);
        try {
            if (operatorPassWord.equals(operator.getPassword())) {
                result = operatorDao.updateOperator(newPassWord, operator.getAccountNumber());
                Date date = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
                logDao.insertloginLog(dateFormat.format(date), "操作员：" + operatorName + "修改密码");
                System.out.println("修改成功！");
            }else {
                System.out.println("原密码错误！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    //管理员忘记密码,找回密码
    public static  int ManagerPassWord(String Number, String newPassWord){
        Manager manager = managerDao.selectAdminByName(Number);
        if (Number.equals(manager.getAccountNumber())) {
            managerDao.updateManager(newPassWord, manager.getAccountNumber());
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            logDao.insertloginLog(dateFormat.format(date) , "管理员：" +Number +"找回密码");
            System.out.println("密码找回成功！");
            return 1;
        } else {
            System.out.println("ID错误！");
        }
        return 0;
    }

    //操作员忘记密码，找回密码
    public static int OperatorPassWord(String operatorNumber, String newPassWord){
        Operator operator = operatorDao.selectBYOperator(operatorNumber);
        if (operator != null) {
            if (operatorNumber.equals(operator.getAccountNumber())) {
                operatorDao.updateOperator(newPassWord, operator.getAccountNumber());
                Date date = new Date();
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
                logDao.insertloginLog(dateFormat.format(date),"操作员：" + operatorNumber +"找回密码");
                System.out.println("密码找回成功！");
                return 1;
            } else {
                System.out.println("操作员ID错误");
            }
        } else {
            System.out.println("操作员用户名不存在");
        }
        return 0;
    }

    //判断存在，判断管理员用户名是否存在
    public static Manager selectName(String name){
        Manager manager = managerDao.selectAdminByName(name);
        if (manager != null) {
            return manager;
        }
        return null;
    }

    //判断操作员用户名是否存在
    public static Operator selectOperatorByName(String operatorName){
        Operator operator = operatorDao.selectBYOperator(operatorName);
        if (operator != null) {
            return operator;
        }
        return null;
    }

    //查询所有的管理员信息
    public static List<Manager> selectAllAdmin() {
        List<Manager> manager = managerDao.selectManager();
        if (manager != null) {
            return manager;
        }
        return null;
    }

    //打印所有的管理员信息
    public static void showAllAdminByName() {
        managerDao.showAllAdminByName();
    }

    //查询所有的操作员信息
    public List<Operator> selectAllOperator() {
        List<Operator> operators = operatorDao.selectOperator();
        if (operators != null) {
            return operators;
        }
        return null;
    }

    //根据操作员名查询
    public static void showAllOperatorByName() {
        operatorDao.selectBYOperator();
    }

    public static void main(String[] args) {
        Login(managerDao.selectBYManager(1),"123456");
    }
}
